package cn.yunhe.controller;

import cn.yunhe.entity.*;
import cn.yunhe.services.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 孙权庆 on 2018/5/25.
 */
@Controller
@RequestMapping("/admin")
public class LoginController {

    @Resource
    private IAdminServices adminServices;

    @Resource
    private IAssistantServices assistantServices;

    @Resource
    private ISchoolServices schoolServices;

    @Resource
    private IStudentServices studentServices;

    @Resource
    private ITeacherServices teacherServices;

    /*登录检查*/
    @RequestMapping("/checkLogin")
    public ModelAndView checkLogin(String username, String password, ModelAndView modelAndView, HttpSession session) {

        Map<String, String> map = new HashMap<>();
        map.put("username", username);
        map.put("password", password);

        Admin admin = adminServices.checkLogin(map);
        Assistant assistant = assistantServices.checkLogin(map);
        School school = schoolServices.checkLogin(map);
        Student student = studentServices.checkLogin(map);
        Teacher teacher = teacherServices.checkLogin(map);

        if (admin != null) {
            session.setAttribute("admin", admin);
            modelAndView.setViewName("/system/admin");
        } else if (assistant != null) {
            session.setAttribute("assistant", assistant);
            modelAndView.setViewName("/assistant/assistant_list");
        } else if (school != null) {
            session.setAttribute("school", school);
            modelAndView.setViewName("/system/school");
        } else if (student != null) {
            session.setAttribute("student", student);
            modelAndView.setViewName("/system/student");
        } else if (teacher != null) {
            session.setAttribute("teacher", teacher);
            modelAndView.setViewName("/system/teacher");
        } else {                 //如果用户名密码错误通过ftl文件夹下的ce跳转到外部login
            modelAndView.addObject("message", "用户名或密码错误，请重新输入！");
            modelAndView.setViewName("/system/skip");
        }
        return modelAndView;
    }
}
